//package com.LachesismStorm.lachesism.member.service;
//
//import com.LachesismStorm.lachesism.core.email.EmailUtil;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.time.LocalTime;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Optional;
//import java.util.concurrent.Executors;
//import java.util.concurrent.ScheduledExecutorService;
//import java.util.concurrent.TimeUnit;
//
//import static com.LachesismStorm.lachesism.core.common.enums.CommonEnum.DATE_FORMATTER;
//import static com.LachesismStorm.lachesism.core.util.LachesismTools.getTime;
//import static com.LachesismStorm.lachesism.core.util.LachesismTools.getTomorrowTime;
//
///**
// * @Author lachesism
// * @Date 2024-12-25 14:31
// **/
//@Service
//public class SchedulerService {
//    @Resource
//    private EmailUtil emailUtil;
//    @Resource
//    private YunOService yunoService;
//
//    // 定时任务调度器
//    private final ScheduledExecutorService hourlyScheduler = Executors.newScheduledThreadPool(2);
//    private final ScheduledExecutorService dailyScheduler = Executors.newScheduledThreadPool(1);
//    private final ScheduledExecutorService constellationScheduler = Executors.newScheduledThreadPool(1);
//    private final ScheduledExecutorService addOldCalendarScheduler = Executors.newScheduledThreadPool(1);
//    private final ScheduledExecutorService sendLunarEmailDelayScheduler = Executors.newScheduledThreadPool(1);
//    private final ScheduledExecutorService sendConstellationEmailDelayScheduler = Executors.newScheduledThreadPool(1);
//
//
//
//    // 计算初始延迟
//    private long calculateInitialDelay(LocalTime now, LocalTime targetTime) {
//        long nowSeconds = now.toSecondOfDay();
//        long targetSeconds = targetTime.toSecondOfDay();
//        // 如果当前时间已经过了目标时间，返回第二天的延迟
//        return nowSeconds >= targetSeconds ? 24 * 60 * 60 - (nowSeconds - targetSeconds) : targetSeconds - nowSeconds;
//    }
//
//    // 启动定时任务
//    public void startScheduledTasks(Runnable autoUpload, Runnable getDailyVideo, Runnable clearTempFiles, Runnable getConstellationList, Runnable addOldCalendar) {
//        // 每小时任务（整点执行）
//        long hourlyInitialDelay = calculateInitialDelayToNextFullHour(LocalTime.now());  // 计算到下一个整点的初始延迟
//        hourlyScheduler.scheduleAtFixedRate(autoUpload, hourlyInitialDelay, 60 * 60, TimeUnit.SECONDS);  // 每小时整点执行
//        hourlyScheduler.scheduleAtFixedRate(getDailyVideo, hourlyInitialDelay, 60 * 60, TimeUnit.SECONDS);  // 每小时整点执行
////
////        // 每小时任务
////        long hourlyInitialDelay = calculateInitialDelay(LocalTime.now(), LocalTime.of(3, 1));
////        hourlyScheduler.scheduleAtFixedRate(autoUpload, hourlyInitialDelay, 60 * 60, TimeUnit.SECONDS);
////        hourlyScheduler.scheduleAtFixedRate(getDailyVideo, hourlyInitialDelay, 60 * 60, TimeUnit.SECONDS);
//
//        // 每天00:00:01任务
//        long dailyInitialDelay = calculateInitialDelay(LocalTime.now(), LocalTime.of(0, 0, 1));
//        dailyScheduler.scheduleAtFixedRate(clearTempFiles, dailyInitialDelay, 24 * 60 * 60, TimeUnit.SECONDS);
//
//        // 每天01:00:01任务
//        long commonInitialDelay = calculateInitialDelay(LocalTime.now(), LocalTime.of(1, 0, 1));
//        constellationScheduler.scheduleAtFixedRate(getConstellationList, commonInitialDelay, 24 * 60 * 60, TimeUnit.SECONDS);
//        addOldCalendarScheduler.scheduleAtFixedRate(addOldCalendar, commonInitialDelay, 24 * 60 * 60, TimeUnit.SECONDS);
//
//        // 每天22:00:01任务
//        long sendLunarEmailDelay = calculateInitialDelay(LocalTime.now(), LocalTime.of(22, 0, 1));
//        sendLunarEmailDelayScheduler.scheduleAtFixedRate(createSendLunarEmail(), sendLunarEmailDelay, 24 * 60 * 60, TimeUnit.SECONDS);
//
//        // 每天6:00:01任务
//        long sendConstellationEmailDelay = calculateInitialDelay(LocalTime.now(), LocalTime.of(6, 0, 1));
//        sendConstellationEmailDelayScheduler.scheduleAtFixedRate(createSendConstellationEmail(), sendConstellationEmailDelay, 24 * 60 * 60, TimeUnit.SECONDS);
//    }
//
//    // 计算当前时间到下一个整点的初始延迟
//    private long calculateInitialDelayToNextFullHour(LocalTime now) {
//        long nowSeconds = now.toSecondOfDay();
//        long nextFullHourSeconds = (now.getHour() + 1) * 60 * 60;  // 下一整点的秒数
//        return nextFullHourSeconds - nowSeconds;  // 当前时间到下一整点的秒数
//    }
//
//    // 关闭所有调度器
//    public void shutdownSchedulers() {
//        hourlyScheduler.shutdown();
//        dailyScheduler.shutdown();
//        constellationScheduler.shutdown();
//        addOldCalendarScheduler.shutdown();
//    }
//
//
//
//}
